package com.deyuanyun.pic.settings.service.prvlg.impl;

import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.domain.prvlg.UserGroup;
import com.deyuanyun.pic.settings.mapper.prvlg.RoleMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.UserGroupMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.UserMapper;
import com.deyuanyun.pic.settings.service.prvlg.UserGroupService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserGroupServiceImpl implements UserGroupService {
	
	//未删除
	private static final short UNDELETE = 0;
	//已删除
	private static final short DELETE = 1;
	// 锁定
	private static final String LOCKED = "LOCKED";
	// 激活
	private static final String ACTIVATE ="ACTIVATE";

	@Resource
	private UserGroupMapper userGroupMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private RoleMapper roleMapper;
	@Override
	public List getUserGroupManagerIndex() {
		Map<String,Object> praMap = new HashMap<String,Object>();
		praMap.put("mark", "group");
		return userGroupMapper.selectAllUserGroup(praMap);
	}

	@Override
	public int saveGroup(String name, String code, String remark,String id,String state) {
		UserSessionVO uv = SessionCacheSupport.getSessionVO();
		int i = 0;
		Map<String,Object> praMap = new HashMap<String,Object>();
		praMap.put("name", name);
		praMap.put("id", id);
		List<UserGroup> ls = userGroupMapper.selectUserGroupByName(praMap);
		if(0 < ls.size() ){
			Asserts.assertTrue(false, "名称已经存在，请重新输入");
		}else if(StringUtils.isEmpty(id)){
			UserGroup ug = new UserGroup();
			ug.setName(name);
			ug.setCode(code);
			ug.setState(state);
			ug.setDeleted(UNDELETE);
			ug.setCreateUser(uv.getId());
			ug.setCreateRemark(remark);
			i = userGroupMapper.insertSelective(ug);
		}else{
			UserGroup ug = userGroupMapper.selectByPrimaryKey(id);
			ug.setName(name);
			ug.setCode(code);
			ug.setUpdateDate(DateUtil.getCurrentDate());
			ug.setUpdateUser(uv.getId());
			ug.setCreateRemark(remark);
			i = userGroupMapper.updateByPrimaryKeySelective(ug);
		}
		return i;
	}

	@Override
	public Map<String, Object> groupAuthorize(String id) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserGroup userGroup = userGroupMapper.selectByPrimaryKey(id);
		//查询该用户组是否已经配置了角色
		List<String> role = userGroupMapper.selectRoleByUserGroupId(id);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < role.size(); i++) {
			if (i == role.size() - 1) {
				sb.append(role.get(i));
			} else {
				sb.append(role.get(i)).append(",");
			}
		}
		List allRole = userGroupMapper.selectAllRole();
		map.put("userGroup", userGroup);
		map.put("allRole", allRole);
		map.put("role", sb.toString());
		return map;
	}

	@Override
	public int saveAuthorize(String id, String[] data) {
		int count = 0;
		// 删除该用户组所属的原来的角色
		count = userGroupMapper.deleteAuthorize(id);
		for (String roleId : data) {
			if(!StringUtils.isEmpty(roleId)){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", UUID.randomUUID().toString().replace("-", ""));
				map.put("userGroupId", id);
				map.put("roleId", roleId);
				userGroupMapper.saveAuthorize(map);
				count ++;
			}
		}
		return count;
	}

	@Override
	public Map<String, Object> addOrModify(String id) {
		int count = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("locked", LOCKED);
		List allUser = userMapper.getUserManagerIndex(map);
		map.put("allUser", allUser);
		if(!StringUtils.isEmpty(id)){
			map.put("userGroup", userGroupMapper.selectByPrimaryKey(id));
		}
		map.put("count", count);
		return map;
	}

	@Override
	public int deleteData(String idStr) {
		int count = 0;
		String[] idArr = idStr.split(",");
		for (String id : idArr) {
			UserGroup ug = userGroupMapper.selectByPrimaryKey(id);
			if(null != ug && LOCKED.equals(ug.getState())){
				ug.setDeleted(DELETE);
				userGroupMapper.updateByPrimaryKeySelective(ug);
				count ++;
			}
		}
		return count;
	}

	@Override
	public int changeState(String userGroupId, String state) {
		int i = 0;
		UserGroup ug = userGroupMapper.selectByPrimaryKey(userGroupId);
		if(null != ug){
			ug.setState(state);
			i = userGroupMapper.updateByPrimaryKeySelective(ug);
		}
		return i;
	}

	@Override
	public int dataOnOrOff(String idStr, String opt) {
		int count = 0;
		String[] idArr = idStr.split(",");
		if("on".equals(opt)){
			for (String id : idArr) {
				UserGroup ug = userGroupMapper.selectByPrimaryKey(id);
				if(LOCKED.equals(ug.getState())){
					ug.setState(ACTIVATE);
					userGroupMapper.updateByPrimaryKeySelective(ug);
					count ++;
				}
			}
		}
		if("off".equals(opt)){
			for (String id : idArr) {
				UserGroup ug = userGroupMapper.selectByPrimaryKey(id);
				if(ACTIVATE.equals(ug.getState())){
					ug.setState(LOCKED);
					userGroupMapper.updateByPrimaryKeySelective(ug);
					count ++;
				}
			}
		}
		return count;
	}

	@Override
	public List searchUserGroup(String name, String code) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", name);
		map.put("code", code);
		List list = userGroupMapper.searchUserGroup(map);
		return list;
	}
}
